package com.xteam.youin_supplier.fragment;


import android.media.MediaActionSound;
import android.os.Bundle;
import android.app.Fragment;
import android.support.annotation.RequiresPermission;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonSyntaxException;
import com.xteam.youin_supplier.MainActivity;
import com.xteam.youin_supplier.R;
import com.xteam.youin_supplier.config.GlobalData;
import com.xteam.youin_supplier.config.RequestAddr;
import com.xteam.youin_supplier.model.BaseModel;
import com.xteam.youin_supplier.model.PrivateInfo;
import com.xteam.youin_supplier.model.UserInfo;
import com.xteam.youin_supplier.util.MethedUtil;
import com.xteam.youin_supplier.util.http.AjaxCallBack;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;

/**
 * 参与竞标
 */
@EFragment(R.layout.fragment_join_bedding)
public class JoinBeddingFragment extends BaseFragment {

    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @ViewById(R.id.price)
    EditText price;
    @ViewById(R.id.percentage)
    EditText percentage;
    @ViewById(R.id.instroduce)
    EditText instroduce;

    @ViewById(R.id.price_layout)
    RelativeLayout priceLayout;
    @ViewById(R.id.percentage_layout)
    RelativeLayout percentageLayout;

    @ViewById(R.id.percentage_select_btn)
    ImageButton percentageBtn;
    @ViewById(R.id.price_select_btn)
    ImageButton priceBtn;

    @FragmentArg("auction_id")
    int auction_id;


    @AfterViews
    @Override
    void initView() {
        backBtn.setVisibility(View.VISIBLE);
        topTitle.setText(getResources().getString(R.string.jogin_bedding_title));
        priceBtn.performClick();
    }

    @Click({R.id.back_btn, R.id.submit_bedding_btn, R.id.percentage_select_btn, R.id.price_select_btn})
    @Override
    void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStack();
                break;
            case R.id.submit_bedding_btn:
                if (checkValidata()) {
                    submitBedding();
                }
                break;
            case R.id.price_select_btn:
                if (!priceBtn.isSelected()) {
                    price.setEnabled(true);
                    percentage.setEnabled(false);
                    priceBtn.setSelected(true);
                    percentageBtn.setSelected(false);
                    priceLayout.setSelected(true);
                    percentageLayout.setSelected(false);
                }
                break;
            case R.id.percentage_select_btn:
                if (!percentageBtn.isSelected()) {
                    price.setEnabled(false);
                    percentage.setEnabled(true);
                    percentageBtn.setSelected(true);
                    priceBtn.setSelected(false);
                    priceLayout.setSelected(false);
                    percentageLayout.setSelected(true);
                }
                break;
        }
    }

    private boolean checkValidata() {
        if (instroduce.getText().toString().trim().equals("")) {
            Toast.makeText(getActivity(), "请填写完整", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    private void submitBedding() {
        String priceStr = "0";
        String pricePercen = "0.00";
        if (priceBtn.isSelected()) {
            priceStr = price.getText().toString().trim();
            if (MethedUtil.isCheckedSpeChar(getActivity(), price.getText().toString())
                   ) {
                return;
            }
            if (priceStr.equals("")) {
                Toast.makeText(getActivity(), "请填写竞拍价格", Toast.LENGTH_SHORT).show();
                return;
            }
        } else {
            if (percentage.getText().toString().trim().equals("")) {
                Toast.makeText(getActivity(), "请填写竞拍下浮指数", Toast.LENGTH_SHORT).show();
                return;
            }
            pricePercen = percentage.getText().toString().trim();
            if (MethedUtil.isCheckedSpeChar(getActivity(), percentage.getText().toString())
                    ) {
                return;
            }
            if (Double.parseDouble(pricePercen) > 0 && Double.parseDouble(pricePercen) < 100 && pricePercen.length() <5){
                pricePercen = (Integer.parseInt(pricePercen) / 100.00) + "";
            }else{
                percentage.setText("");
                Toast.makeText(getActivity(), "输入不合法，请重新输入", Toast.LENGTH_SHORT).show();
                return;
            }

        }
        requestParams = "account=" + GlobalData.userInfo.getData().getAccount()
                + "&auction_id=" + auction_id
                + "&note=" + instroduce.getText().toString().trim()
//                + "&price=" + price.getText().toString().trim()
                + "&price=" + priceStr
                + "&price1=" + pricePercen
                + "&supplier_id=" + GlobalData.userInfo.getData().getSupplier_id()
                + "&user_id=" + GlobalData.userInfo.getData().getUser_id();
        requestUrl = RequestAddr.addbedding + requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                dealData(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    private void dealData(Object object) {
        try {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
        } catch (JsonSyntaxException e) {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
            return;
        }
        if (!baseModel.isFlag()) {
            return;
        }
        Toast.makeText(getActivity(), baseModel.getMsg(), Toast.LENGTH_SHORT).show();
        MainActivity.getInstance().getSupportFragmentManager().popBackStack();
    }
}
